package com.coolzeng.controller;

import com.alibaba.fastjson.JSON;
import com.coolzeng.entity.JsonResult;
import com.coolzeng.entity.User;
import com.coolzeng.service.UserService;
import com.sun.deploy.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/5.
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

//处理获得所有用户封装成json格式
    @ResponseBody
    @RequestMapping("/users")
        public JsonResult<List<User>> userlist(){
            List<User> allUser = userService.GetAllUser();
        JsonResult<List<User>> jsonResult = new JsonResult<>(allUser, "This is a message.", true);
            return jsonResult;
        }

//json数据返回
    @ResponseBody
    @RequestMapping("/users1")
    public JSON userlist1(Model model){
        List<User> allUser =userService.GetAllUser();
        model.addAttribute("allUser", allUser);
        JSON allUser1= (JSON) JSON.toJSON(allUser);
        return allUser1;
    }
//根据id查询用户
    @RequestMapping("/user/{var}")
    @ResponseBody
    public JsonResult<List<User>> user(@PathVariable("var") int var){
        List <User> user=userService.Getuser(var);

        JsonResult<List<User>> userResult =
                new JsonResult<>(user, "This is a message.", true);

        return userResult;
    }

    //返回用户列表
    @RequestMapping(value = "user")
    public String userlist(Model model){
        model.addAttribute("allUser",userService.GetAllUser());
        return "userlist";
    }

//新建用户
    @ResponseBody
    @RequestMapping(value = "/user/add",method = RequestMethod.POST )
    public JSON useradd(int id, String username, String password, int age){
        User user=new User();
        user.setId(id);
        user.setUserName(username);
        user.setPassword(password);
        user.setAge(age);
        userService.AddUser(user);
        JSON json= (JSON) JSON.toJSON(user);

       /* System.out.println("++++++++++++++++++++"++"+++++++++++++++++++++++");*/
        return json;
    }

//根据id删除用户
    @ResponseBody
    @RequestMapping(value = "/delete/{uid}",produces = "application/json;charset=utf-8")
    public String deleteUser(@PathVariable("uid")int uid){
        userService.DeleteUser(uid);
        return "删除id为："+uid+"的用户成功";
    }

    //更新用户信息

    @RequestMapping("/user/update")
    public String updateUser(int id,String username,String password,int age,Model model){
        User user=new User();
        user.setId(id);
        user.setUserName(username);
        user.setPassword(password);
        user.setAge(age);
        userService.UpdateUser(user);
        model.addAttribute(user);
        return "userlist";
    }


    //测试封装返回数据
    @RequestMapping("/json")
    @ResponseBody
    public Object returnJson() {
        List uesrs= (List) userService.Getuser(99);
        // 组装 查询的 结果 , 添加消息 和 是否成功的标识
        JsonResult<List<String>> jsonResult = new JsonResult<>(uesrs, "This is a message.", true);
        Object jsonResult2= JSON.toJSON(jsonResult);
        System.out.println("++++++++++"+JSON.toJSON(jsonResult)+"++++++++++");
       /* // 复杂一点的 封装
        Object[] objs = { jsonResult, jsonResult2, jsonResult3 };
        JsonResult<Object[]> jsonObj = new JsonResult<Object[]>(objs);*/
        return jsonResult2;
    }
}
